//
//  SZRecommendMainController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZRecommendMainController.h"
#import <SDCycleScrollView.h>
#import "PKRequestManager.h"
#import "SZRecommendModel.h"
#import "SZRecommendMainCell.h"
#import "SZRecommendDetailViewController.h"
#import <MJRefresh.h>
#import "CBStoreHouseRefreshControl.h"
#define kRecommendMainURL @"http://morguo.com/forum.php?mod=movieexplorer&v=3&androidflag=1&appfrom=ios&iosversion=3.13&page=1&pdateline=0"

@interface SZRecommendMainController ()
/** 无限轮播图 */
@property (nonatomic, strong) SDCycleScrollView *scrollView;
/** 轮播图数组 */
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;
@end

@implementation SZRecommendMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSMutableArray array];
    _dataSource = [NSMutableArray array];
    self.tableView.backgroundColor = [UIColor blackColor];
    [self.tableView registerClass:[SZRecommendMainCell class] forCellReuseIdentifier:@"SZRecommendMainCell"];
    [self setupData];
    [self setupTableViewDataWithURLString:@"http://news-at.zhihu.com/api/4/theme/3"];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        SZRecommendModel *model = _dataSource.lastObject;
        NSString *string = [NSString stringWithFormat:@"http://news-at.zhihu.com/api/4/theme/3/before/%@",model.ID];
        [self setupTableViewDataWithURLString:string];
    }];
    
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.tableView target:self refreshAction:@selector(refreshTriggered:) plist:@"KJ" color:[UIColor whiteColor] lineWidth:1.5 dropHeight:80 scale:1 horizontalRandomness:150 reverseLoadingAnimation:YES internalAnimationFactor:0.5];
}

/** 
 *  添加轮播图大表头
 */
- (void)addHeaderScrollViewWithArray:(NSArray *)imageArray {
    _scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 2) imageURLStringsGroup:imageArray];
    _scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.tableView.tableHeaderView = _scrollView;
}

/**
 *  解析表头数据
 */
- (void)setupData {
    [PKRequestManager requestWithUrlString:kRecommendMainURL parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        NSArray *imageTempArray = dataDic[@"data"][@"explorerbannerlist"];
        for (NSDictionary *subDic in imageTempArray) {
            NSString *tempString = [NSString stringWithFormat:@"http://morguo.com/%@",subDic[@"image"]];
            [_imageArray addObject:tempString];
        }
        [self addHeaderScrollViewWithArray:_imageArray];
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}

/**
 *  解析tableView数据
 */
- (void)setupTableViewDataWithURLString:(NSString *)urlString {
    [PKRequestManager requestWithUrlString:urlString parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        NSMutableArray *array = [SZRecommendModel mj_objectArrayWithKeyValuesArray:dataDic[@"stories"]];
        [_dataSource addObjectsFromArray:array];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SZRecommendMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SZRecommendMainCell"];
    cell.model = _dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (kScreenWidth - 30) / 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SZRecommendDetailViewController *detailVC = [[SZRecommendDetailViewController alloc] init];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.ID = [_dataSource[indexPath.row] ID];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)refreshTriggered:(id)sender {
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl {
    [self.storeHouseRefreshControl finishingLoading];
}
#pragma mark - Notifying refresh control of scrolling

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.storeHouseRefreshControl scrollViewDidScroll];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}
@end
